<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <script src="static/js/jquery-2.1.1.min.js"></script>
    <%@include file="../layUI.jsp"%>

</head>
<body>
<form action="doSmall?action=smallUpdate" method="post">
    <di id="d1"></di>
    小分类ID<input type="text" value="" name="id" readonly>
    小分类名称<input type="text" value="" name="smallName" id="smallName" class="notNull" notNull="小分类名称">
    大分类名称<select name="smallBigId" id="smallBigId" >
     <%--   <option value="-1">全部</option>--%>
    </select>
    小分类描述<input type="text" value="" name="smallText" class="notNull" notNull="小分类描述">
    <input type="button" value="修改完成" class="layui-btn" id="sub"  onclick="saves()">
</form>
<script type="text/javascript">
    $(function () {
        getSmallList();
    })
    var id ;
    var bigId;
    //渲染数据
    function getSmallList() {
        id = '${param.id}';
        $.getJSON("/doSmall",{"action":"selById","id":id},callback);
        function callback(data) {
            $("[name=id]").val(data[0].smallId);
            $("[name=smallName]").val(data[0].smallName);
            $("[name=smallText]").val(data[0].smallText);
            bigId = data[0].bigClass.bigName;
            getbigName();
        }
    }
    //select选择
    function getbigName(){
        $.getJSON("/doSmall",{"action":"selSmallBigName"},callback);
        function callback(data) {
            $(data).each(function (i) {
                if(bigId==this.bigName){
                    $("#smallBigId").append(" <option selected value='"+(i+1)+"'>"+this.bigName+"</option>")
                }else{
                    $("#smallBigId").append(" <option value='"+(i+1)+"'>"+this.bigName+"</option>")
                }
            })
        }
    }

    //保存信息
    function saves() {
        //非空验证
        var flag = true;
        $(".notNull").each(function () {
            var name = $(this).attr("name");
            if($(this).val()==""){
                alert($(this).attr('notNull')+"不能为空");
                flag =false;
                return;
            }
        })
        if(flag){
            //得到所有值，并转为字符串
            var queryString = $("form").serialize();
            $.getJSON("/doSmall?action=smsUpdate&id="+id, queryString, callback);
            function callback(data) {
                if (data.flag == "true") {
                    alert("修改成功!");
                    location.href="/page/smallclass/SmallQuery.jsp";
                } else {
                    alert("修改失败!");
                    location.href="/page/bigclass/bigUpdate.jsp";
                }
            }
        }

    }
</script>
</body>
</html>
